package com.kaifamiao.demo;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lucien-ma
 * @date 2021/10/13
 * @apiNote
 */
public class Demo02 {

    public static void main(String[] args) {
        InputStream inputStream = Demo02.class.getClassLoader().getResourceAsStream("com/kaifamiao/demo/schoolInfo.xml");
        try {
            School school = parseSchoolInfoXML(inputStream);
            System.out.println(school);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static School parseSchoolInfoXML(InputStream inputStream) throws Exception {
        // 创建 SAXReader 对象
        SAXReader saxReader = new SAXReader();
        // 获取 document 对象
        Document document = saxReader.read(inputStream);
        // 获取根结点
        Element rootElement = document.getRootElement();
        // 创建 school 对象
        School school = new School();
        // 获取 name
        Element nameElement = rootElement.element("name");
        String name = nameElement.getTextTrim();
        school.setName(name);
        // 获取地址
        Element addressElement = rootElement.element("address");
        String address = addressElement.getTextTrim();
        school.setAddress(address);
        // 创建班级对象
        Classes classes = new Classes();
        // 获取班级节点
        Element classesElement = rootElement.element("classes");
        // 获取班级名称
        Element classesNameElement = classesElement.element("name");
        String classesName = classesNameElement.getTextTrim();
        classes.setName(classesName);
        // 创建存放学生的集合
        List<Student> studentList = new ArrayList<>();
        // 获取学生大节点(students)
        Element studentsElement = classesElement.element("students");
        // 获取 students 下的多个 student
        List<Element> students = studentsElement.elements("student");
        for (Element student : students) {
            // 创建学生对象
            Student s = new Student();
            // 获取学生姓名
            Element studentNameElement = student.element("name");
            String studentName = studentNameElement.getTextTrim();
            s.setName(studentName);
            // 获取学生学号
            Element studentNoElement = student.element("stuNo");
            String studentNo = studentNoElement.getTextTrim();
            s.setStuNO(studentNo);
            // 获取学生性别
            Element studentGenderElement = student.element("gender");
            String studentGender = studentGenderElement.getTextTrim();
            s.setGender(studentGender);
            // 添加到集合中
            studentList.add(s);
        }
        // 给 classes 对象的学生集合属性设置值
        classes.setStudents(studentList);
        // 创建存放 Classes 对象的集合
        List<Classes> classesList = new ArrayList<>();
        // 添加值
        classesList.add(classes);
        // 给 school 对象的 ClassesList 属性设置值
        school.setClassesList(classesList);
        return school;
    }
}
